<?php
/**
 * Created by PhpStorm.
 * User: lzy
 * Date: 2018/11/23
 * Time: 17:43
 */
namespace App\FreeBird\Repositories;
use App\FreeBird\Contracts\InfoComplaintInterface;
use App\FreeBird\Exceptions\FreeBirdException;
use App\FreeBird\Models\InfoComplaintModel;
use App\FreeBird\Models\InfoModel;
use App\Weixin\Contracts\UserInterface;
use App\Weixin\Models\WeixinUserModel;
use Illuminate\Support\Facades\Request;

/**
 * 订单投诉操作
 * Class DbInfoComplaintRepositories
 */
class  DbInfoComplaintRepositories implements InfoComplaintInterface
{

    /**
     * 用户接口
     * @return UserInterface
     */
    public function getUserInterface(){
        return app()->make(UserInterface::class);
    }

    /**
     * 投诉模型
     * @return InfoComplaintModel
     */
    public function getInfoComplaintModel(){
        return app()->make(InfoComplaintModel::class);
    }
    /**
     * 微信用户模块
     * @return WeixinUserModel
     */
    public function getWeixinUserModel(){
        return app()->make(WeixinUserModel::class);
    }

    /**
     * 微信用户模块
     * @return InfoModel
     */
    public function getInfoModel(){
        return app()->make(InfoModel::class);
    }
    /**
     * 添加数据
     * @param $data
     */
   public function updateAndCreate($data){
         $data["order_sn"]=empty($data["order_sn"])?null: $data["order_sn"];
         return $this->getInfoComplaintModel()->newQuery()->updateOrCreate(["order_sn"=>$data["order_sn"]],$data);
   }

    /**
     * 根据条件查询单条
     * @param $where
     * @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Model|null|object
     */
   public function findInfo($where){
       return $this->getInfoComplaintModel()->newQuery()->where($where)->first();
   }

    /**
     * 根据id查询
     * @param $id
     * @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Model|null|object
     */
   public function findById($id){
       return $this->getInfoComplaintModel()->newQuery()->where("id",$id)->first();
   }

    /**
     * 根据订单号获取投诉信息
     * @param $orderSn
     */
    public function getInfoWithOder($orderSn){
        return $this->getInfoComplaintModel()->newQuery()->with(["order"])->where("order_sn",$orderSn)->first();
    }

    /**
     * 获取订单列表
     * @param $where
     */
    public function getList($where)
    {
        $request = Request::instance();
        $limit = empty($request->input("limit")) ? 10 : $request->input("limit");
        $sort = empty($request->input("sort")) ? 'create_time' : $request->input("sort");
        $order = empty($request->input("order")) ? 'desc' : $request->input("order");
        return $this->getInfoComplaintModel()->newQuery()->where($where)->orderBy($sort, $order)->paginate($limit);
    }
    /**
     * 获取订单列表
     * @param $where
     */
    public function getListWith($where)
    {

        $request = Request::instance();
        $limit = empty($request->input("limit")) ? 10 : $request->input("limit");
        $sort = empty($request->input("sort")) ? 'create_time' : $request->input("sort");
        $order = empty($request->input("order")) ? 'desc' : $request->input("order");
        return $this->getInfoComplaintModel()->newQuery()
            ->select([$this->getInfoModel()->table.".*",$this->getWeixinUserModel()->table.".truename",$this->getWeixinUserModel()->table.".mobile",$this->getInfoComplaintModel()->table.".apply_status as c_status"])
            ->leftJoin($this->getInfoModel()->table, $this->getInfoComplaintModel()->table . '.order_sn', '=', $this->getInfoModel()->table . '.order_sn')
            ->leftJoin($this->getWeixinUserModel()->table, $this->getWeixinUserModel()->table . '.uid', '=', $this->getInfoModel()->table . '.uid')
            ->where($where)->orderBy($this->getInfoComplaintModel()->table.".".$sort, $order)->paginate($limit);
    }


    /**
     * 获取详细
     * @param $data
     * @return mixed|void
     */
    public function getShowByOrderSn($order_sn){

        $where=function($query) use ($order_sn){
            $query->where($this->getInfoComplaintModel()->table.".order_sn",$order_sn);
        };
        $request = Request::instance();
        $sort = empty($request->input("sort")) ? 'create_time' : $request->input("sort");
        $order = empty($request->input("order")) ? 'desc' : $request->input("order");
        return $this->getInfoComplaintModel()->newQuery()->with("user","buser")
            ->select([$this->getInfoComplaintModel()->table.".*",$this->getInfoModel()->table.".*",$this->getWeixinUserModel()->table.".truename",$this->getWeixinUserModel()->table.".mobile",$this->getInfoComplaintModel()->table.".apply_status as c_status"])
            ->leftJoin($this->getInfoModel()->table, $this->getInfoComplaintModel()->table . '.order_sn', '=', $this->getInfoModel()->table . '.order_sn')
            ->leftJoin($this->getWeixinUserModel()->table, $this->getWeixinUserModel()->table . '.uid', '=', $this->getInfoModel()->table . '.uid')
            ->where($where)->orderBy($this->getInfoComplaintModel()->table.".".$sort, $order)->first();
    }


    /**
     * 用户余额比率
     * @param $data
     * @return mixed|void
     */
    public function sendBiliUserBalance($data){
        $bili=$data["bili"];
        $orderSn=$data["order_sn"];
        $applyMsg=$data["apply_msg"];
        $info=$this->getInfoModel()->newQuery()->where("order_sn",$orderSn)->first();
        if($info->order_status!='5'){
            throw  new  FreeBirdException(FreeBirdException::EROOR_INFO_UPDATE_ORDERSTATUS);  //状态不对
        }
        if($this->getInfoComplaintModel()->newQuery()->where("order_sn",$orderSn)->update(["apply_status"=>1,"apply_msg"=>$applyMsg])){
            //开始记算金钱
            $mount=$info->all_amount;
            $userMoney=(100-$bili)/100*$mount;  //普通会员求邦比例
            if(!empty($userMoney)){
                $this->getUserInterface()->updateUserBalance($info->uid,$userMoney,8,$info->order_sn,'投诉后台审核，返还');  //更新支付状态
            }
            $makeMoney=($bili*get_config_param_value("TS_PUSH_BONUS_BACK"))/10000*$mount;  //入驻会员的返比例
            if(!empty($makeMoney)){
                $this->getUserInterface()->updateUserBalance($info->buid,$makeMoney,9,$info->order_sn,'投诉后台审核，服务返还');  //更新支付状态
            }
            //消息
            //添加消息---产品确定不需要发消息
            return  array();
        }
    }
}